<?php

class School_Model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * @param $row
     * @return School
     */
    public static function getSchoolByRow($row)
    {
        $thatSchool = new School;
        $thatSchool->setId($row->id);
        $thatSchool->setName($row->name);
        $thatSchool->setUnicode($row->unicode);
        $thatSchool->setCountry($row->country);
        $thatSchool->setProvince($row->province);
        $thatSchool->setCity($row->city);
        $thatSchool->setRegion($row->region);
        $thatSchool->setAddress($row->address);
        $thatSchool->setAuthType($row->auth_type);
        $thatSchool->setIpRange($row->ip_range);

        return $thatSchool;
    }

    /**
     * @param School $thatSchool
     * @return array
     */
    public static function getModelBySchool($thatSchool)
    {
        return array(
            'name'      => $thatSchool->getName(),
            'unicode'   => $thatSchool->getUnicode(),
            'country'   => $thatSchool->getCountry(),
            'province'  => $thatSchool->getProvince(),
            'city'      => $thatSchool->getCity(),
            'region'    => $thatSchool->getRegion(),
            'address'   => $thatSchool->getAddress(),
            'auth_type' => $thatSchool->getAuthType(),
            'ip_range' => $thatSchool->getIpRange(),
        );
    }

    /**
     * @param int $id
     * @return School
     */
    public static function getSchoolById($id)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->select('school.*');
        $CI->db->from('school');
        $CI->db->where('school.id', $id);

        $query = $CI->db->get();
        $row   = $query->row();
        if ($row)
            return self::getSchoolByRow($row);
        else
            return null;
    }

    /**
     * @param int $unicode
     * @return School
     */
    public static function getSchoolByUnicode($unicode)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->select('school.*');
        $CI->db->from('school');
        $CI->db->where('school.unicode', $unicode);

        $query = $CI->db->get();
        $row   = $query->row();
        if ($row)
            return self::getSchoolByRow($row);
        else
            return null;
    }

    /**
     * @param bool|int $limit
     * @param bool|int $offset
     * @return array
     */
    public static function getSchoolArray($limit = false, $offset = false)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->select('school.*');
        $CI->db->from('school');
        $CI->db->order_by('id', 'asc');

        if ($limit !== false && is_numeric($limit)) {
            if ($offset === false || !is_numeric($offset))
                $offset = 0;
            $CI->db->limit($limit, $offset);
        }

        $query  = $CI->db->get();
        $result = $query->result();

        $array = array();
        foreach ($result as $row) {
            $array[] = self::getSchoolByRow($row);
        }
        return $array;
    }

    /**
     * @param School $thatSchool
     * @throws DatabaseException
     * @return School
     */
    public static function add($thatSchool)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $result = $CI->db->insert('school', self::getModelBySchool($thatSchool));
        if ($result)
            return self::getSchoolById($CI->db->insert_id());
        else
            throw new DatabaseException('插入数据失败');
    }

    /**
     * @param School $thatSchool
     * @throws DatabaseException
     * @return School
     */
    public static function update($thatSchool)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->where('id', $thatSchool->getID());
        $result = $CI->db->update('school', self::getModelBySchool($thatSchool));
        if ($result)
            return self::getSchoolById($thatSchool->getID());
        else
            throw new DatabaseException('更新数据失败');
    }

    /**
     * @param School $thatSchool
     * @throws DatabaseException
     * @return bool
     */
    public static function delete($thatSchool)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->where('id', $thatSchool->getID());
        $result = $CI->db->delete('school');
        if ($result)
            return true;
        else
            throw new DatabaseException('删除数据失败');
    }

}
